{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 模型构建中的概念\n",
    "## tensor        数据，即某一类型的多维数组\n",
    "## variable      用于定义模型中的参数，是通过不断训练得到的值\n",
    "## placeholder   输入变量的载体。也可以理解成定义函数时的参数\n",
    "## operation,OP  即一个OP获得0个或多个tensor，执行计算，输出额外的0个或多个tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'Hello, TensorFlow!'\n"
     ]
    }
   ],
   "source": [
    "# 编写hello world程序演示session的使用\n",
    "import tensorflow as tf\n",
    "hello = tf.constant('Hello, TensorFlow!')\n",
    "with tf.Session() as sess:\n",
    "    print (sess.run(hello))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "相加：7\n",
      "相乘：12\n",
      "[12, 7]\n"
     ]
    }
   ],
   "source": [
    "# 使用注入机制\n",
    "## 将具体的实参注入到相应的placeholder中。feed只在调用它的方法内有效，方法结束后feed就会消失\n",
    "a = tf.placeholder(tf.int16)\n",
    "b = tf.placeholder(tf.int16)\n",
    "add = tf.add(a, b)\n",
    "mul = tf.multiply(a, b)\n",
    "with tf.Session() as sess:\n",
    "    print ('相加：%i' % sess.run(add, feed_dict={a:3, b:4}))\n",
    "    print ('相乘：%i' % sess.run(mul, feed_dict={a:3, b:4}))\n",
    "    # 使用注入机制将定义在图中的节点数值取出来\n",
    "    print (sess.run([mul, add], feed_dict={a:3, b:4}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "按行降维结果：\n",
      " [2 2 2]\n",
      "按列降维结果：\n",
      " [3 3]\n"
     ]
    }
   ],
   "source": [
    "# 规约计算\n",
    "## 规约计算的操作都会有降维的功能，在所有reduce_xxx系列操作函数中，都是以xxx的手段降维，\n",
    "## 每个函数都有axis这个参数，即沿某个方向，使用xxx方法对输入tensor进行降维\n",
    "## axis默认值是None，即把input_tensor降到0维，即一个数\n",
    "X = tf.Variable([[1, 1, 1,],\n",
    "                 [1, 1, 1]])\n",
    "# 按行降维\n",
    "X_row = tf.reduce_sum(X, axis=0)\n",
    "# 按列降维\n",
    "X_col = tf.reduce_sum(X, axis=1)\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    print ('按行降维结果：\\n', sess.run(X_row))\n",
    "    print ('按列降维结果：\\n', sess.run(X_col))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
